package com.itheima.reggie.common;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Long employee =(Long) request.getSession().getAttribute("employee");
        Long user =(Long) request.getSession().getAttribute("user");
        if(employee!=null)
        {
            BaseTest.set(employee);
            return true;
        }
        if(user!=null)
        {
            BaseTest.set(user);
            return true;
        }
        String s = JSON.toJSONString(R.error("NOTLOGIN"));
        response.getWriter().write(s);
        return false;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        BaseTest.remove();
    }
}
